# -*- coding: utf-8 -*-
from datetime import datetime, time
import numpy as np
import pandas as pd

def is_month_first(date, year, month):
    '''
    判断某天是否是某年某月的 01 号
    date 是带判断的时间变量
    '''
    if date.month == month:
        if date.day == 1:
            return True
        else:
            return False
    else:
        return False

def is_in_period(t, time_1, time_2):
    '''
    判断某个时间是否落在两个时间点内。
    '''
    if time_1 <= t and t < time_2:
        return True

def judge_time_period(t_series, time_0, time_8, time_16, time_24):
    '''
    判断时间序列中，属于三个阶段的个数
    '''
    count = []
    count_num = np.sum((t_series < time_8) & (t_series >= time_0))
    count.append(count_num)
    count_num = np.sum((t_series < time_16) & (t_series >= time_8))
    count.append(count_num)
    count_num = np.sum((t_series < time_24) & (t_series >= time_16))
    count.append(count_num)
    
    for i in range(3):
        print(f'属于第{i+1}阶段发生的事故次数为{count[i]}')
    
    
def return_num(data):
    years = [16, 17, 18, 19, 20]
    months = [2, 5, 8, 11]
    time_0 = time(0, 0)
    time_8 = time(8, 0)
    time_16 = time(16, 0)
    time_24 = time(23, 59)
    date = data.接警日期
    for year in years:
        for month in months:
            # 判断是否属于某月的第一天
            print(f'年份：{year}, 月份：{month}')
            data = data.loc[date.apply(lambda x: is_month_first(x, year, month))]
            judge_time_period(data.接警时间点, time_0, time_8, time_16, time_24)
    
            

if __name__ == '__main__':
    path = r'../附件/附件2：某地消防救援出警数据.xlsx'
    time_series = pd.read_excel(path)
    return_num(time_series)
